<template>
  <div class='container'>
    <el-dialog
      :title="form.id ? '修改文章' : '新增文章'"
      :visible="visible"
      @close="close"
      @open="open"
    >
      <!-- 表单 -->
      <el-form label-width="80px" ref="addForm" :rules="rules" :model="form">
        <!-- 标题 -->
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title"/>
        </el-form-item>
        <!-- 富文本 -->
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            ref="myTextEditor"
            v-model="form.articleBody"
            :options="editorOption"
            @change="onEditorChange($event)"
          />
        </el-form-item>
        <!-- 视频地址 -->
        <el-form-item label="视频地址">
          <el-input v-model="form.videoURL"/>
        </el-form-item>
      </el-form>
      <!-- 页脚按钮 -->
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="add">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, detail } from '@/api/hmmm/articles'

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'ArticlesAdd',
  components: {
    quillEditor
  },
  props: {
    visible: {
      required: true,
      type: Boolean
    },
    nodeData: {
      required: true,
      type: Object
    }
  },
  data () {
    return {
      // 表格数据
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 校验规则
      rules: {
        title: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      editorOption: {
        placeholder: '编辑文章内容'
      }
    }
  },
  methods: {
    close () {
      this.$emit('update:visible', false)
      this.$emit('update:nodeData', {})
      this.$refs.addForm.resetFields()
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 发请求
    async add () {
      try {
        await this.$refs.addForm.validate()
      } catch {
        return false
      }
      if (this.form.id) {
        await update(this.form)
      } else {
        await add(this.form)
      }
      this.$emit('getList')
      this.$message.success(this.form.id ? '修改成功' : '添加成功')
      this.close()
    },
    async open () {
      // console.log(this.nodeData.id)
      if (!this.nodeData.id) return
      const { data } = await detail(this.nodeData)
      // console.log(data)
      this.form = data
    },
    onEditorChange ({ editor, html, text }) {
      // console.log(editor, html, text, 1)
      this.form.articleBody = html
    }
  }
}
</script>

<style scoped lang='scss'>
  ::v-deep {
    .el-dialog__body {
      overflow: hidden;
      .ql-container {
        height: 300px;
      }
    }
  }
</style>
